import json, openpyxl

def read_json():
    with open("./data/attapi.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    
    return data

def process_data(data):
    username = data["data"].get("memberName")
    workbook = openpyxl.load_workbook('./data/因公外出明细表1.xlsx')
    sheet = workbook.worksheets[0]
    active_row = 3
    for ins in data["data"]["dateList"]:
        if ins.get("date"):
            date = ins.get("date")  # 有些数据是空的, 含义不明
            if ins.get("err_date_list") and len(ins.get("err_date_list")):
                # 只分成三种情况: 上午没来, 下午没来, 全天没来
                err = []
                for err_ins in ins.get("err_date_list"):
                    if "上班" in err_ins["context"]:
                        err.append("上午")
                    if "下班" in err_ins["context"]:
                        err.append("下午")
                
                if "上午" in err and "下午" in err:
                    print(f"{username} {date} 全天没来")
                    cell = sheet[f'A{active_row}']
                    cell.value = username
                    cell = sheet[f'C{active_row}']
                    cell.value = f"{date} 08:30:00"
                    cell = sheet[f'D{active_row}']
                    cell.value = f"{date} 18:00:00"
                    cell = sheet[f'E{active_row}']
                    cell.value = "1.00"
                elif "上午" in err:
                    print(f"{username} {date} 上午没来")
                    cell = sheet[f'A{active_row}']
                    cell.value = username
                    cell = sheet[f'C{active_row}']
                    cell.value = f"{date} 08:00:00"
                    cell = sheet[f'D{active_row}']
                    cell.value = f"{date} 14:00:00"
                    cell = sheet[f'E{active_row}']
                    cell.value = "0.56"
                elif "下午" in err:
                    print(f"{username} {date} 下午没来")
                    print(f"{username} {date} 上午没来")
                    cell = sheet[f'A{active_row}']
                    cell.value = username
                    cell = sheet[f'C{active_row}']
                    cell.value = f"{date} 11:00:00"
                    cell = sheet[f'D{active_row}']
                    cell.value = f"{date} 18:30:00"
                    cell = sheet[f'E{active_row}']
                    cell.value = "0.75"
                
                active_row += 1
                
    workbook.save('./data/因公外出明细表1.xlsx')    
    input("处理完毕, 打开./data/因公外出明细表1.xlsx查看结果, 按任意键退出.")
                    
                    
                


if __name__ == "__main__":
    data = read_json()
    process_data(data)
    


